package com.biz.primus.management.promotion.tag;

import com.biz.primus.model.promotion.enums.PromotionPointGiftTypeEnum;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

/**
 * 会员积分增长方式
 *
 * @author david-liu
 */
@Setter
@Slf4j
public class PromotionPointGiftTypeTag extends TagSupport {
    private static final long serialVersionUID = -7906239151478140958L;

    private String selectedType;

    @Override
    public int doStartTag() throws JspException {
        JspWriter jspWriter = pageContext.getOut();
        for (PromotionPointGiftTypeEnum giftType : PromotionPointGiftTypeEnum.values()) {
            String out;
            if (StringUtils.isNotBlank(selectedType) && StringUtils.equalsIgnoreCase(selectedType, giftType.name())) {
                out = String.format("<option value=\"%s\" selected>%s</option>", giftType.name(), giftType.getDesc());
            } else {
                out = String.format("<option value=\"%s\">%s</option>", giftType.name(), giftType.getDesc());
            }
            try {
                jspWriter.println(out);
            } catch (IOException e) {
                log.error("输出用户积分增长类型JSP Tag异常", e);
            }
        }
        return super.doStartTag();
    }
}
